<?php
require_once(dirname(dirname(__FILE__)) . '/app.php');
//print_r($HTTP_POST_VARS);
$shopList = Shop::GetAllShops();


if( $defaultShopId < 1 ){
	$defaultShopId = 1;
}
$shop = Shop::getById($defaultShopId);

$time = $_GET['time'];





//get system year and month
$tmp_date=date("Ymd");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
if( $time == null ){
	$time = array(
		'startYear' => $tmp_year,
		'startMonth' => 1,
		'startDay' => 1,
		'endDay' => 1,
	);
}

$yearList = array($tmp_year, $tmp_year + 1);

if($time['startYear'] == $tmp_year){
	$time['startYear'] = $tmp_year;
}

if( $time['startYear'] == $tmp_year && $time['startMonth'] < $tmp_mon ){
	$time['startMonth'] = $tmp_mon;
}

$startTime = mktime(0, 0, 0, $time['startMonth'], $time['startDay'], $time['startYear']);
$now = time() + 0;
if( $startTime < $now ){
	$time['startYear'] = $tmp_year;
	$time['startDay'] = $tmp_day;
	$time['endDay'] = $tmp_day + 7;
	if( $time['endDay'] > date("t") ){
		$time['endDay'] = date("t");
	}
}
$workDayList = array();
for($index = $time['startDay']; $index < $time['endDay'] + 1; $index++){
	$workDayList[$index] = $index;
};
$staffList = Staff::GetAllByShop($defaultShopId);
$workScheduleStaffId = array();
foreach($staffList as $k => $v){
	$tempList = array();
	for($index = $time['startDay']; $index < $time['endDay'] + 1; $index++){
		$tempList[ $index ] = Absence::GetByPeriod($time['startYear'], $time['startMonth'], $index, $index, $v['id']);
		$workDayList[$index] = $index;
		//a day of week
		$workDayofWeekList[$index] = getYoubi($time['startYear'].sprintf("%02d",$time['startMonth']).sprintf("%02d",$index));
		$workDayofWeekList[$index] = mb_convert_encoding($workDayofWeekList[$index],"UTF-8", "SJIS-win");
	};
	$workScheduleStaffId[$v['id']] = $tempList; 
}

$monthList = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$dayList = array();
for($index = 1; $index <= 31; $index++){
	array_push($dayList, $index);
}

$hourList = array();
for($index = $shop['open_hour']; $index <= $shop['close_hour'] + 1; $index++){
	array_push($hourList, $index);
}

$minuteList = array();
for($index = 0; $index <= 60; $index=$index+5){
	array_push($minuteList, $index);
}

include template('/service/staff_unwork_time');

function getYoubi($date){
    $sday = strtotime($date);
    $res = date("w", $sday);
    $day = array("sun", "mon", "tues", "wed", "thurs", "fri", "sat");
    return $day[$res];
}